<?php
include_once ("/includes/SimpleXmlExtended.class.php");
include_once ("includes/diccionario.class.php");

class LatexCompleto {
	private $documentclass;
	private $usepackage;
	private $configuracion;
	private $documento = Array (
		'inicio' => '',
		'portada' =>'',
		'indice' => '',
		'fin' => ''
	);
	private $tablas = Array();
	
	private $tabla = Array (
			'iniciotabla' => '',
			'filanombre' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaambito' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filadefinicion' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaacepcionvenezuela' => Array (
				'pais' => '',
				'divcolum' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaejemplovenezuela' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filafuentevenezuela' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaacepcioncolombia' => Array (
				'pais' => '',
				'divcolum' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaejemplocolombia' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filafuentecolombia' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaacepcionecuador' => Array (
				'pais' => '',
				'divcolum' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaejemploecuador' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filafuenteecuador' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaacepcionperu' => Array (
				'pais' => '',
				'divcolum' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filaejemploperu' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'filafuenteperu' => Array (
				'multicolumn' => '',
				'texto' => '',
				'finfila' => ''
			),
			'captacion' => Array ('comando' => '', 'nombre' => ''),
			'fintabla' => ''
	);
	private $rutaXML = "datos/latexCompleto.xml";
	private $xml;

	# CONSTRUCTOR

	public function __construct() {
		$this->cargarXML();
	}

	#CARGAMOS XML  

	private function cargarXML() {
		$this->xml = simplexml_load_file($this->rutaXML);
	}

	#DEFINIMOS LOS GETS

	public function getDocumentclass() {
		return $this->documentclass;
	}

	public function getUsepackage() {
		return $this->usepackage;
	}

	public function getConfiguracion() {
		return $this->configuracion;
	}

	public function getDocumento() {
		return $this->documento;
	}

	public function getImagen() {
		return $this->imagen;
	}

	public function getTabla() {
		return $this->tabla;
	}
	
	public function getTablas() {
		return $this->tablas;
	}

	#DEFINIMOS LOS SETS

	public function setDocumentclass($documentclass) {
		$this->documentclass = $documentclass;
	}

	public function setUsepackage($usepackage) {
		$this->usepackage = $usepackage;
	}

	public function setConfiguracion($configuracion) {
		$this->configuracion = $configuracion;
	}

	public function setDocumento($documento) {
		$this->documento = $documento;
	}

	public function setImagen($imagen) {
		$this->imagen = $imagen;
	}

	public function setTabla($tabla) {
		$this->tabla = $tabla;
	}
	
	public function setTablas($tablas) {
		$this->tablas = array_merge($this->tablas,$tablas);
	}

	public function extraerLatex() {
		$resul = $this->xml->xpath('/*');
		$this->getLatexCompleto($resul);
	}

	private function getLatexCompleto(Array $latexXMLlist) {
		$latexXML2 = $latexXMLlist[0];
		$this->documentclass = (string) $latexXML2->documentclass;
		$this->usepackage = (string) $latexXML2->usepackage;
		$this->configuracion = (string) $latexXML2->configuracion;
		
		$latexXMLl = $latexXML2->contenido;
		$latexXML = $latexXMLl[0];
		
		$this->documento['inicio'] = (string) $latexXML->inicio;
		$this->documento['portada'] = (string) $latexXML->portada;
		$this->documento['indice'] = (string) $latexXML->indice;
		$this->documento['fin'] = (string) $latexXML->fin;
		
		$dicionario = new Diccionario();
		$lista_registros = $dicionario->listarRegistro();
		for ($i = 0; $i < count($lista_registros); $i++) {
			
			$termino = $lista_registros[$i];
			$this->tabla['iniciotabla'] = (string) $latexXML->tabla[0]->iniciotabla;
			$filal = $latexXML->tabla[0]->filanombre;
			$fila = $filal[0];
			
			$this->tabla['filanombre']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filanombre']['texto'] =  "{".$this->caracteresLatex((string)$termino['nombre'])."}";
			$this->tabla['captacion']['nombre'] = "{".$this->caracteresLatex((string)$termino['nombre'])."}";
			$this->tabla['filanombre']['finfila'] = (string) $fila->finfila;
	
			$filal = $latexXML->tabla[0]->filaambito;
			$fila = $filal[0];
			
			$this->tabla['filaambito']['multicolumn'] = (string) $fila->multicolumn;
			$ambito = $termino['ambito'];
			$this->tabla['filaambito']['texto'] ="";
			foreach ( $ambito as $clave => $valor ) {
       			$this->tabla['filaambito']['texto'] = $this->tabla['filaambito']['texto'] . ", " . $this->caracteresLatex((string)$ambito[$clave]);
			}
			$this->tabla['filaambito']['texto'] = substr($this->tabla['filaambito']['texto'], 2);
			$this->tabla['filaambito']['texto'] = "{" . $this->tabla['filaambito']['texto'] . "}";
			$this->tabla['filaambito']['finfila'] = (string) $fila->finfila;
	
			$filal = $latexXML->tabla[0]->filadefinicion;
			$fila = $filal[0];
			
			$this->tabla['filadefinicion']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filadefinicion']['texto'] = "{".$this->caracteresLatex((string)$termino['definicion'])."}";
			$this->tabla['filadefinicion']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaacepcionvenezuela;
			$fila = $filal[0];
			
			$this->tabla['filaacepcionvenezuela']['pais'] = (string) $fila->pais;
			$this->tabla['filaacepcionvenezuela']['divcolum'] = (string) $fila->divcolum;
			$this->tabla['filaacepcionvenezuela']['texto'] = "{".$this->caracteresLatex((string)$termino['venezuela']['acpcion'])."}";
			$this->tabla['filaacepcionvenezuela']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaejemplovenezuela;
			$fila = $filal[0];
			
			$this->tabla['filaejemplovenezuela']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filaejemplovenezuela']['texto'] = "{".$this->caracteresLatex((string)$termino['venezuela']['ejemplo'])."}";
			$this->tabla['filaejemplovenezuela']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filafuentevenezuela;
			$fila = $filal[0];
			
			$this->tabla['filafuentevenezuela']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filafuentevenezuela']['texto'] = "{".$this->caracteresLatex((string)$termino['venezuela']['fuente'])."}";
			$this->tabla['filafuentevenezuela']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaacepcioncolombia;
			$fila = $filal[0];
			
			$this->tabla['filaacepcioncolombia']['pais'] = (string) $fila->pais;
			$this->tabla['filaacepcioncolombia']['divcolum'] = (string) $fila->divcolum;
			$this->tabla['filaacepcioncolombia']['texto'] = "{".$this->caracteresLatex((string)$termino['colombia']['acepcion'])."}";
			$this->tabla['filaacepcioncolombia']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaejemplocolombia;
			$fila = $filal[0];
			
			$this->tabla['filaejemplocolombia']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filaejemplocolombia']['texto'] = "{".$this->caracteresLatex((string)$termino['colombia']['ejemplo'])."}";
			$this->tabla['filaejemplocolombia']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filafuentecolombia;
			$fila = $filal[0];
			
			$this->tabla['filafuentecolombia']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filafuentecolombia']['texto'] = "{".$this->caracteresLatex((string)$termino['colombia']['fuente'])."}";
			$this->tabla['filafuentecolombia']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaacepcionecuador;
			$fila = $filal[0];
			
			$this->tabla['filaacepcionecuador']['pais'] = (string) $fila->pais;
			$this->tabla['filaacepcionecuador']['divcolum'] = (string) $fila->divcolum;
			$this->tabla['filaacepcionecuador']['texto'] = "{".$this->caracteresLatex((string)$termino['ecuador']['acepcion'])."}";
			$this->tabla['filaacepcionecuador']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaejemploecuador;
			$fila = $filal[0];
			
			$this->tabla['filaejemploecuador']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filaejemploecuador']['texto'] = "{".$this->caracteresLatex((string)$termino['ecuador']['ejemplo'])."}";
			$this->tabla['filaejemploecuador']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filafuenteecuador;
			$fila = $filal[0];
			
			$this->tabla['filafuenteecuador']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filafuenteecuador']['texto'] = "{".$this->caracteresLatex((string)$termino['ecuador']['fuente'])."}";
			$this->tabla['filafuenteecuador']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaacepcionperu;
			$fila = $filal[0];
			
			$this->tabla['filaacepcionperu']['pais'] = (string) $fila->pais;
			$this->tabla['filaacepcionperu']['divcolum'] = (string) $fila->divcolum;
			$this->tabla['filaacepcionperu']['texto'] = "{".$this->caracteresLatex((string)$termino['peru']['acepcion'])."}";
			$this->tabla['filaacepcionperu']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filaejemploperu;
			$fila = $filal[0];
			
			$this->tabla['filaejemploperu']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filaejemploperu']['texto'] = "{".$this->caracteresLatex((string)$termino['peru']['ejemplo'])."}";
			$this->tabla['filaejemploperu']['finfila'] = (string) $fila->finfila;
			
			$filal = $latexXML->tabla[0]->filafuenteperu;
			$fila = $filal[0];
			
			$this->tabla['filafuenteperu']['multicolumn'] = (string) $fila->multicolumn;
			$this->tabla['filafuenteperu']['texto'] = "{".$this->caracteresLatex((string)$termino['peru']['fuente'])."}";
			$this->tabla['filafuenteperu']['finfila'] = (string) $fila->finfila;
			
			$this->tabla['captacion']['comando'] = (string) $latexXML->tabla[0]->captacion[0]->comando;
			$this->tabla['fintabla'] = (string) $latexXML->tabla[0]->fintabla;
			
			$this->tablas[$i] = $this->tabla;
		}

	}


	private function caracteresLatex($cadena) {
		$cadena = preg_replace('/%/', '\\\\\%', $cadena);
		$cadena = preg_replace('/{/', '\\\\\{', $cadena);
		$cadena = preg_replace('/}/', '\\\\\}', $cadena);
		$cadena = preg_replace('/~/', '$\\\\\~$', $cadena);
		$cadena = preg_replace('/\$/', '$\\\\\\\$$', $cadena);
		return $cadena;
	}

}
?>
